package com.iflytek.autofly.setting.biz;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.iflytek.autofly.noticemanager.NoticeManagerClient;

import java.util.concurrent.atomic.AtomicInteger;


/**
 * <b>Description: </b>TODO<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/12/17</td>
 * <td>20:06</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class Notice {
    private static final java.lang.String TAG = "Notice";
    NoticeManagerClient mNoticeManager;
    private Context mContext;
    private AtomicInteger mCount = new AtomicInteger(0);

    private Notice() {
    }

    public static Notice getInstatnce() {
        return NoticeSingleton.inst;
    }

    public void init(final Context context) {
        mContext = context;
        mNoticeManager = new NoticeManagerClient(context, new NoticeManagerClient.IBindServiceListener() {
            @Override
            public void onServiceDisconnected() {
                mNoticeManager = null;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        init(mContext);
                    }
                }).start();
            }

            @Override
            public void onServiceConnected() {

            }
        });

    }

    public void showWithBroadcast(String content, boolean tts, Intent intent) {
        if (mNoticeManager != null) {
            PendingIntent pi = PendingIntent.getBroadcast(mContext, mCount.getAndIncrement(), intent
                    , PendingIntent.FLAG_UPDATE_CURRENT);
            mNoticeManager.showNotice(content, tts, pi, null);
        } else {
            init(mContext);
        }
    }

    public void showWithActivity(String content, boolean tts, Intent intent) {
        if (mNoticeManager != null) {
            PendingIntent pi = PendingIntent.getActivity(mContext, mCount.getAndIncrement(), intent
                    , PendingIntent.FLAG_UPDATE_CURRENT);
            mNoticeManager.showNotice(content, tts, pi, null);
        } else {
            init(mContext);
        }
    }

    public void show(String content, boolean tts, PendingIntent pi) {
        if (mNoticeManager != null) {
            mNoticeManager.showNotice(content, tts, pi, null);
        } else {
            init(mContext);
        }
    }

    public void show(String content, boolean tts, PendingIntent active, PendingIntent negative) {
        if (mNoticeManager != null) {
            mNoticeManager.showNotice(content, tts, active, negative);
        } else {
            init(mContext);
        }
    }

    public void show(String content, boolean tts) {
        if (mNoticeManager != null) {
            mNoticeManager.showToast(content, tts);
        } else {
            init(mContext);
        }
    }

    private static class NoticeSingleton {
        static Notice inst = new Notice();
    }

}
